package com.ljh.mq.springrabbitmqdemo.config;

import com.ljh.mq.springrabbitmqdemo.constants.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: Themberfue
 * @date: 2025/4/28 20:36
 * @description:
 */
@Configuration
public class RabbitMQConfig {
    // * 工作队列模式
    @Bean
    public Queue workQueue() {
        return QueueBuilder.durable(Constants.WORK_QUEUE)
                .build();
    }

    // * 发布订阅模式
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange() {
        return ExchangeBuilder.fanoutExchange(Constants.FANOUT_EXCHANGE)
                .durable(true)
                .build();
    }
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1 () {
        return QueueBuilder.durable(Constants.FANOUT_QUEUE1)
                .build();
    }
    @Bean("fanoutQueue2")
    public Queue fanoutQueue2 () {
        return QueueBuilder.durable(Constants.FANOUT_QUEUE2)
                .build();
    }
    @Bean("bindingFanout1")
    public Binding bindingFanout1(@Qualifier("fanoutExchange") FanoutExchange exchange, @Qualifier("fanoutQueue1") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange);
    }
    @Bean("bindingFanout2")
    public Binding bindingFanout2(@Qualifier("fanoutExchange") FanoutExchange exchange, @Qualifier("fanoutQueue2") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange);
    }

    // * 路由模式
    @Bean("directExchange")
    public DirectExchange directExchange() {
        return ExchangeBuilder.directExchange(Constants.DIRECT_EXCHANGE)
                .durable(true)
                .build();
    }
    @Bean("directQueue1")
    public Queue directQueue1() {
        return QueueBuilder.durable(Constants.DIRECT_QUEUE1)
                .build();
    }
    @Bean("directQueue2")
    public Queue directQueue2() {
        return QueueBuilder.durable(Constants.DIRECT_QUEUE2)
                .build();
    }
    @Bean("bindingDirect1")
    public Binding bindingDirect1(@Qualifier("directExchange") DirectExchange exchange, @Qualifier("directQueue1") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("orange");
    }
    @Bean("bindingDirect2")
    public Binding bindingDirect2(@Qualifier("directExchange") DirectExchange exchange, @Qualifier("directQueue2") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("orange");
    }
    @Bean("bindingDirect3")
    public Binding bindingDirect3(@Qualifier("directExchange") DirectExchange exchange, @Qualifier("directQueue2") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("black");
    }

    // * 通配符模式
    @Bean("topicExchange")
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange(Constants.TOPIC_EXCHANGE)
                .durable(true)
                .build();
    }
    @Bean("topicQueue1")
    public Queue topicQueue1() {
        return QueueBuilder.durable(Constants.TOPIC_QUEUE1)
                .build();
    }
    @Bean("topicQueue2")
    public Queue topicQueue2() {
        return QueueBuilder.durable(Constants.TOPIC_QUEUE2)
                .build();
    }
    @Bean("bindingTopic1")
    public Binding bindingTopic1(@Qualifier("topicExchange") TopicExchange exchange, @Qualifier("topicQueue1") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("*.orange.*");
    }
    @Bean("bindingTopic2")
    public Binding bindingTopic2(@Qualifier("topicExchange") TopicExchange exchange, @Qualifier("topicQueue2") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("*.*.rabbit");
    }
    @Bean("bindingTopic3")
    public Binding bindingTopic3(@Qualifier("topicExchange") TopicExchange exchange, @Qualifier("topicQueue2") Queue queue) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("lazy.#");
    }
}
